/*
 * Aresta.java
 *
 * Created on 12 de Junho de 2007, 19:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jerm.Interface.JGraph_Diagrama;

import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.GraphConstants;

/**
 * 
 * @author tiagofalcao
 */
public class Aresta extends DefaultEdge {
	// public static int CARDINALIDADE_01=GraphConstants.ARROW_LINE;
	// public static int CARDINALIDADE_11=GraphConstants.ARROW_DOUBLELINE;
	// public static int CARDINALIDADE_0N=GraphConstants.ARROW_CIRCLE;
	// public static int CARDINALIDADE_1N=GraphConstants.ARROW_CIRCLE;
	public static int HERANCA = GraphConstants.ARROW_CLASSIC;

	// public static boolean CARDINALIDADEP_01=false;
	// public static boolean CARDINALIDADEP_11=false;
	// public static boolean CARDINALIDADEP_0N=false;
	// public static boolean CARDINALIDADEP_1N=true;
	public static boolean HERANCAP = false;

	public static int LINE_SIMPLES = 0;
	public static int LINE_ORTOGONAL = GraphConstants.STYLE_ORTHOGONAL;
	public static int LINE_BEIZER = GraphConstants.STYLE_BEZIER;
	public static int LINE_SPLINE = GraphConstants.STYLE_SPLINE;

	public static int TARGET = 2;
	public static int SOURCE = 1;
	public static int ANY = 0;

	protected static String viewclassAresta = "Interface.JGraph_Diagrama.ArestaView";
	protected static String viewclassArestaTotal = "Interface.JGraph_Diagrama.ArestaTotalView";
	protected String viewclass;

	protected boolean Heranca = false;
	protected int min = 0;
	protected int max = 2;
	protected String Label;

	protected boolean Total = false;

	/**
	 * Constructs an empty edge.
	 */
	public Aresta() {
		this(null);
	}

	/**
	 * Constructs an edge that holds a reference to the specified user object.
	 * 
	 * @param userObject
	 *            reference to the user object
	 */
	public Aresta(Object userObject) {
		this(userObject, null);
	}

	/**
	 * BlocoGraphCell Constructs an edge that holds a reference to the specified
	 * user object and sets default values for points and the label position.
	 * 
	 * @param userObject
	 *            reference to the user object
	 */
	public Aresta(Object userObject, AttributeMap storageMap) {
		super(userObject, storageMap);
		Label = new String();
		viewclass = Aresta.viewclassAresta;
		JGraphCellViewFactory.setViewClass(this.getAttributes(), viewclass);
		setAtributos();
	}

	public void setAtributos() {
		AttributeMap map = new AttributeMap();
		// Set
		GraphConstants.setDisconnectable(map, false);
		// GraphConstants.setMoveable(map,false);
		GraphConstants.setResize(map, false);
		// Add a label along edge attribute
		GraphConstants.setLabelAlongEdge(map, true);
		// Block Edit
		GraphConstants.setEditable(map, false);

		getAttributes().applyMap(map);
	}

	public void setCardinalidade(int min, int max) {
		AttributeMap map = getAttributes();

		if (min == 0 && max == 1) {
			Label = "(0,1)";
			// GraphConstants.setLineEnd(map, CARDINALIDADE_01);
			// GraphConstants.setEndFill(map, CARDINALIDADEP_01);

		} else if (min == 1 && max == 1) {
			Label = "(1,1)";
			// GraphConstants.setLineEnd(map, CARDINALIDADE_11);
			// GraphConstants.setEndFill(map, CARDINALIDADEP_11);

		} else if (min == 0 && max > 1) {
			Label = "(0,N)";
			// GraphConstants.setLineEnd(map, CARDINALIDADE_0N);
			// GraphConstants.setEndFill(map, CARDINALIDADEP_0N);

		} else if (min == 1 && max > 1) {
			Label = "(1,N)";
			// GraphConstants.setLineEnd(map, CARDINALIDADE_1N);
			// GraphConstants.setEndFill(map, CARDINALIDADEP_1N);

		}
		GraphConstants.setLineEnd(map, GraphConstants.ARROW_NONE);
		GraphConstants.setEndFill(map, false);

		// Object[] labels = {Label};//,new String("1")};
		// Point2D[] labelPositions ={ new Point2D.Double
		// (GraphConstants.PERMILLE*15/16,-20 )};//, new Point2D.Double
		// (GraphConstants.PERMILLE/8, -20);
		// GraphConstants.setExtraLabelPositions(getAttributes(),
		// labelPositions);
		// GraphConstants.setExtraLabels(getAttributes(), labels);
		//            
		GraphConstants.setValue(getAttributes(), Label);
		GraphConstants.setLabelAlongEdge(map, true);

		this.min = min;
		this.max = max;
		Heranca = false;
	}

	public void setHeranca() {
		AttributeMap map = getAttributes();

		this.min = 0;
		this.max = 0;
		Heranca = true;

		GraphConstants.setLineEnd(map, HERANCA);
		GraphConstants.setEndFill(map, HERANCAP);

	}

	public boolean isHeranca() {
		return Heranca;
	}

	public int getMin() {
		return min;
	}

	public int getMax() {
		return max;
	}

	public void setStyle(int LINE) {
		GraphConstants.setLineStyle(getAttributes(), LINE);
	}

	public void setTotal(boolean Total) {
		this.Total = Total;
		if (Total) {
			viewclass = Aresta.viewclassArestaTotal;
			GraphConstants.setValue(getAttributes(), "");

		} else {
			viewclass = Aresta.viewclassAresta;
			setCardinalidade(min, max);
		}
		JGraphCellViewFactory.setViewClass(this.getAttributes(), viewclass);
	}

	public boolean isTotal() {
		return Total;
	}
}
